#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>

typedef struct msgbuf{
	long type;
	int id;
	char name[32];
	int score;
}MSG;

#define MSGLEN (sizeof(MSG) - sizeof(long))

int main(int argc ,const char * argv[]){
	key_t key = ftok(".",100);
	if(key < 0){
		fprintf(stdout,"ftok error\n");
		exit(-1);
	}

	int msgid = msgget(key,IPC_CREAT | 0777);
	if(msgid < 0){
		fprintf(stdout,"ftok error\n");
                exit(-1);
	}

	system("clear");
	system("ipcs -q");
	puts("****************************************");
	
	MSG msg;
	memset(&msg,0,sizeof(MSG));

	if (msgrcv(msgid,&msg,MSGLEN,2,0)==-1){
		printf("msgrcv error\n");
		exit(-1);
	}
		
	printf("%ld - %d - %s - %d\n", msg.type, msg.id, msg.name, msg.score);

	system("ipcs -q");
	return 0;
}
